package com.tianxiao.test.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.baijia.tianxiao.sqlbuilder.SingleSqlBuilder;
import com.baijia.tianxiao.sqlbuilder.bean.Expression;
import com.baijia.tianxiao.sqlbuilder.support.JdbcTemplateDaoSupport;
import com.baijia.tianxiao.sqlbuilder.util.Expressions;
import com.tianxiao.test.dao.ConflictCheckDao;
import com.tianxiao.test.model.ConflictCheck;

@Repository
public class ConflictCheckDaoImp extends JdbcTemplateDaoSupport<ConflictCheck> implements ConflictCheckDao {

	

	@Override
	public List<ConflictCheck> queryConflictByCondition(ConflictCheck conflictCheck) {
		SingleSqlBuilder<ConflictCheck> builder = createSqlBuilder();
		//builder.select(null);
		if(conflictCheck.getStartTime()!=null){
			builder.gt("endTime", conflictCheck.getStartTime());
		}
		if(conflictCheck.getEndTime()!=null){
			builder.lt("startTime", conflictCheck.getEndTime());
		}
		List<Expression> orExpressionList = new ArrayList<Expression>();
		if(conflictCheck.getCourseId() !=null){
			orExpressionList.add(Expressions.eq("courseId",conflictCheck.getCourseId()));
		}
		if(conflictCheck.getClassroomId() != null){
			orExpressionList.add(Expressions.eq("classroomId",conflictCheck.getClassroomId()));
		}
		if(conflictCheck.getTeacherId() != null){
			orExpressionList.add(Expressions.eq("teacherId",conflictCheck.getTeacherId()));
		}	
		
		builder.and(Expressions.or(orExpressionList.toArray(new Expression[]{})));
		
		return queryList(builder);
		
	}

	@Override
	public List<ConflictCheck> queryByConditonRange(ConflictCheck conflictCheck) {
		SingleSqlBuilder<ConflictCheck> builder = createSqlBuilder();
		//builder.select(queryProp);
		
		builder.between("startTime", conflictCheck.getRangeStart(), conflictCheck.getRangeEnd());
		List<Expression> orExpressionList = new ArrayList<Expression>();
		
		if(conflictCheck.getCourseId() !=null){
			orExpressionList.add(Expressions.eq("courseId",conflictCheck.getCourseId()));
		}
		if(conflictCheck.getClassroomId() != null){
			orExpressionList.add(Expressions.eq("classroomId",conflictCheck.getClassroomId()));
		}
		if(conflictCheck.getTeacherId() != null){
			orExpressionList.add(Expressions.eq("teacherId",conflictCheck.getTeacherId()));
		}	
		
		builder.and(Expressions.or(orExpressionList.toArray(new Expression[]{})));
		
		return queryList(builder);
	}

}
